<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class accounts_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$data=array();

	} 	
		
		
public function  get_customers(){
		$this->db->select('customer.*');
	$this->db->from('customer');
	//$this->db->join('users_groups', 'users.id= users_groups.user_id AND users_groups.group_id= 4  '  );
	//$this->db->join('company', 'product.id_company= company.id');
	$q = $this->db->get();
		//$q = $this->db->get_where('customer',array(''=>'');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
		//	print_r($data);exit;	
			return $data;
		}
	}
	

public function get_invoiceno($id_user){
		$this->db->select('accounts.*');
		$q = $this->db->get_where('accounts',array('id_user'=>$id_user,'type'=>1,'credit_amount' => 0));
		if($q->num_rows() > 0) {
			$data =$q->result_array();
			$o='';	
			//print_r($data);exit;
			$first = '
<option value=""> -- Select Invoiceno -- </option>';
foreach ( $data as  $subcategory) 
{
  $o.=  '<option value="'.$subcategory['id'].'" >'.$subcategory['voucher_no'].'</option>';
}
echo $first.$o;
		}

}

public function get_credit_amount($id_acc){
	if($id_acc != 0){
	$this->db->select('accounts.debit_amount');
	$q = $this->db->get_where('accounts',array('id'=>$id_acc));
	$sale = $q->row_array();		
	echo $sale['debit_amount'];exit;	
	}else{
		echo 0;exit;
	}
		
}



public function get_voucherno($id_account){
	if($id_account != 0){
	$this->db->select('accounts.voucher_no');
	$q = $this->db->get_where('accounts',array('id'=>$id_account));
	$purchase = $q->row_array();		
	return 	$purchase['voucher_no'];
	}else{
			return 	0;
	
	}
}

/*public function get_voucherno($id_sale){
	if($id_sale != 0){
		$this->db->select('sales.invoice_no');
		$q = $this->db->get_where('sales',array('id'=>$id_sale));
		$sale = $q->row_array();		
		return 	$sale['invoice_no'];		
	}else{
		return 	0;
	}

}
*/
public function insert($data, $id_sale){
	$data1 = array('status' => 1);
	$this->db->update('sales',$data1,array('id'=>$id_sale));
	if($data['payment_type']=='0'){
		$data['payment_mode'] = 'Cash';
		$data['type'] = '1';
	}else{
		$data['payment_mode'] = 'Cheque/Draft';	
		$data['type'] = '1';
	}
	$this->db->insert('accounts',$data);	
}

public function get_payments(){
	$this->db->select('accounts.*,customer.name as customer_name');
	$this->db->from('accounts','customer');
	$this->db->join('customer', 'accounts.id_user= customer.id AND accounts.voucher_type = "receipt" ' );
	//$this->db->join('company', 'product.id_company= company.id');
	$q = $this->db->get();
	return $q;		

}

function edit_mr($id){
	$this->db->select('accounts.*');
	$q = $this->db->get_where('accounts',array('id'=>$id));
	$sale = $q->row_array();		
	return $sale;
}

function get_id_sale($invoice_no){
	//echo $invoice_no;exit;
	if($invoice_no != ''){
			$this->db->select('sales.id');
			$q = $this->db->get_where('sales',array('invoice_no'=>$invoice_no));
			$sale = $q->row_array();
			//echo $sale['id'];exit;
			return $sale['id'];
	}else{
		return 0;
	}
	
	
}


function update($data, $id){
	if($data['payment_type']=='0'){
		$data['payment_mode'] = 'Cash';
		$data['type'] = '1';
	}else{
		$data['payment_mode'] = 'Cheque/Draft';	
		$data['type'] = '1';
	}
	$this->db->update('accounts',$data,array('id'=>$id));
	
}
function delete_mr($id){

	$this->db->delete('accounts',array('id'=>$id));
	
}

function view_accounts(){
	
	$q = $this->db->get('accounts');
	return $q;	
}

function get_partyname($id){
	
	$q = $this->db->get_where('supplier',array('id'=>$id));
	$user = $q->row()->name;
	return $user; 
}

public function get(){
	   $this->datatables
			->select('accounts.id as id ,accounts.user_name,accounts.voucher_no ,accounts.date, accounts.credit_amount,accounts.payment_mode,accounts.cheque_no,accounts.bank_name')
			->from('accounts')
		//	->join('users', 'accounts.id_user= users.id')
			->where('voucher_type','receipt')
		/*	->add_column("Actions", 	
			"<center>			<a href='edit_mr/$1'>
								<i class='icon-edit'></i>
								</a>
							    <a href='delete_mr/$1' onClick=\"return confirm('". 'Do U Want To Delete Category! ' ."')\">
								<i class='icon-trash'></i>
								</a></center>", "id")*/
			->unset_column('id');
		
		
	   return  $this->datatables->generate();
	
}

public function get_all(){
	   $this->datatables
			->select('accounts.id as id ,accounts.user_name,accounts.voucher_no ,accounts.date,accounts.voucher_type ,accounts.debit_amount, accounts.credit_amount,accounts.payment_mode,accounts.cheque_no,accounts.bank_name')
			->from('accounts')
		//	->join('users', 'accounts.id_user= users.id')
			/*->add_column("Actions", 	
			"<center>			<a href='edit_mr/$1'>
								<i class='icon-edit'></i>
								</a>
							    <a href='delete_mr/$1' onClick=\"return confirm('". 'Do U Want To Delete Category! ' ."')\">
								<i class='icon-trash'></i>
								</a></center>", "id")*/
			->unset_column('id');
		
		
	   return  $this->datatables->generate();
	
}

public function update_mr($data, $id_account){
		if($data['payment_type']=='0'){
		$data['payment_mode'] = 'Cash';
	}else{
		$data['payment_mode'] = 'Cheque/Draft';	
	}
	$this->db->update('accounts',$data,array('id'=>$id_account));


}


}
